Welcome back.
We're going to continue where we left off by starting to fill out our game service.
This is the service that will be responsible for listening when our start game event gets fired by our
player.
Join service.
So as you remember in our last lecture, when enough players join the game and then all of the players
have properly loaded, we go ahead and start the game by firing our start game event, and our game
service is going to be responsible for starting up our game so we can go ahead and open this.
And the five services we're going to need in here is first server storage.
We're going to need server script service.
We're going to need replicated storage.
We'll need the players service.
And then we're going to get the team service.
And then we can create a table.
We'll just call it game service, and we'll make sure to return it at the end of our module script here.
Okay, so the few variables we'll need for now in the service is, well, first we want to require a
module script inside of service script service.
So in here we have a module section.
And there is a module script called game sections.
And this module script is going to hold all of the different functions to execute different sections
of the game.
So for example we'll have a beginning section where all the players must do tasks.
And then we'll have a second section where players must survive against our evil AI Squidward monster
in the basement.
So we'll have a section one and a section two, so we can go ahead and require that.
We'll just call this sections, and it's inside of Server Script Service.
In the server folder, get the modules folder and we'll require the games section module script.
And for now in here we will just create a variable.
We'll just call it game sections.
And we'll return it at the end.
For now.
Now back in our game service, what we're going to do next is we're going to require our GUI actions
enum.
Which is inside of replicated storage inside of the modules folder.
Enums.
Dot GUI actions.
Enums.
We want to have a reference to our Start game event.
And that's going to be inside of server storage, dot events, dot bind labels and get our start game
bind event.
And then we're also going to want to have access to an event called Game Communication inside of replicated
storage.
And this game communication remote event is going to be responsible for allowing the client to communicate
to the server and allowing the server to communicate to the client.
So we'll call this game comms event equal to replicated storage.
Dot events dot remotes, dot game communication.
Now we're going to have to create a standard of communication between the client and the server.
And to do that we can create another custom enumerated type.
So inside of replicated storage, inside of modules and an enum, we have a game communication enum
that will allow us to define the different actions that the server can send to clients, and the different
actions that clients can send to the server.
So we'll make a reference to it here.
We'll call it Game Comms enum and we'll require replicated storage, dot modules, dot enums, dot game
communication enum.
And then we can go ahead and open up this enum.
And we can basically just copy what's in here and paste it in here.
But instead what we want to do is we want to define two key value pairs.
One that defines actions to the server.
So we can call it to server.
And one that defines actions that can be given to the client.
So we can call it two client.
So for example, some actions that can be given to the client would be like playing a sound.
So we can call it play sound.
Another action could be like stopping a particular sound from playing.
Another action we could have is to tell the player to change, uh, the audio effects that they have
on their end.
Because inside of our sound service, we have three different type of sound groups.
One that define the different effects that need to be applied for outdoor sounds, indoor sounds and
sounds in the basement.
So when the player enters the building, we want to muffle any outdoor sounds.
And we don't want to muffle any indoor sounds, so we can create an action like change sound to inside
to let the player know to un, muffle any sounds inside, and muffle any sounds that are outside.
And then we can do the same thing for when they enter the basement.
So change sound to basement.
They'll want to keep any sounds that are outside muffled, as well as any sounds that are inside of
the restaurant, muffled because they are inside of the basement.
Another action we could give to the players is to let them know when to start specific scenes.
So we're going to have different camera scenes happen based on the different sections in our game.
So we have our bus here.
If you remember in the beginning where we're going to have the player's camera attached to this bus,
and this bus is going to drive down this road for about 11 seconds until it reaches its destination
and drops the players off.
So we need to have control over when we want to tell the players to start these different scenes.
So one of the actions could be starting the intro scene.
And we could also have an action for when the players begin to enter the basement.
And we're going to be doing that with, uh, the stairs that the players are going to be walking down
into the basement.
So we can call this start stair scene.
Another action we could give is to tell the players when to apply lightning effects.
So the server is going to be the one that's going to be calculating the lightning, and will let the
players know when to do, like lightning sounds and stuff like that on their end.
And then last but not least, is that we're going to have a system where when the player dies, when
they're in the basement after getting hunted by, uh, our Squidward AI, we want to give them the ability
to respawn back into the game.
So there will be a button that they can press to request to respawn their character.
So in our two server section, we can have an action of like request respawn.
And this is the request players will make when they want to respond.
So when they request this, the server will prompt to them to purchase a developer product.
And when that player purchases the developer product and the server verifies it, then we can tell that
particular player to, you know, tell them that, hey, the respawn was granted.
You can, you know, close out your spectating guy and stuff like that so we can give an action to the
client of like respawn.
Granted.
And that's all the actions that we're going to need inside of our game communication enum for now.
So back in our game service, the next thing that we can make a reference to is our update guy event
and replicated storage.
Dot events, dot remotes, dot update guy.
And then we also want to have a reference to the players loaded event and replicated storage as well.
Because when that other service tells us to start the game, then we want to fire to all of the players
that, hey, we're starting the game and you can go ahead and fade out your loading screens.
And then we're also going to make a reference to a part in the workspace called escape Part.
So this is the part that's going to be at the very end of our game.
After the players have successfully regained power back in the basement, they need to escape to a door
that will open and that door has a part in it.
The players need to touch in order to escape and then once they've escaped, then we can display on
their screen stuff like how much money they earned, how many tasks they completed, and then we can
teleport them back to the lobby.
So we'll make a reference to this escape part, and it's inside of the workspace underneath the Krusty
Krab folder.
And there's a folder in there called basement.
And inside of this folder is our escape part.
Okay.
Now we're going to need some private functions.
One function is going to be for starting our game.
We're going to need a function to listen for when a character is added to a player.
And this is because we want to listen for when that character dies and when that character dies, we
want to set their team to the dead team.
We're also going to want to listen for when a player is added to a team, or more specifically, we
want to listen to when a player is added to the dead team.
Because when this happens, what we want to do is we want to remove any tools that may be inside of
the player's backpack or on their character.
And then this service is only going to need an initialize function, because all the service is going
to do is listen to events.
It doesn't have to start or run any code.
One of those events we need to listen to is our start game event.
So when that gets fired, we of course need to start our game so we can call our start game function.
And we could actually instead connect the start game function itself as a reference to this event.
And what we could do inside of here is we could send out a warning like the game is starting.
And then what we want to do is we want to access our players loaded event and fire to all the clients
in our game that everybody has successfully loaded.
That way they can fade out their loading screens, and then we're going to use our Gamescom event and
fire to all of the clients using the action of game comms.
Enum.
Dot two client dot start the intro scene.
And since this scene is going to require them to attach their cameras to the bus, we can pass a table
with a reference to that bus inside of it.
So we can call.
This bus is equal to workspace Dot outdoor Dot bus.
And since we're going to be tweening this bus for about 11 seconds on the client, and we can't keep
track of those tweens on the server, we're just going to put a wait statement in here that's going
to wait for 11 seconds.
And then after those 11 seconds are up, we're going to use our update UI event and fire to all the
clients that we need to have them update the objectives frame on their screen, because now we need
to add a bunch of objectives for them.
So we'll pass a table.
And inside of this table we're going to have other tables that represent the different objectives they
need to add to their objective guy.
So the name of one of the objectives would be money or AKA this is how much money they need to earn
to pay rent.
And we can pass alongside of this name a key value pair, uh, some text to define what exactly they
need to do for this objective.
And for now, we're just going to leave it blank.
And then we'll have another objective of entering into the building.
So we need to tell the players that, hey, you need to enter into the Krusty Krab.
And we can provide text with this objective as well.
But we're going to leave that blank for now, because what we're going to do is we're going to go back
into our game sections module script.
And in here is where we're going to define the text for some of the objectives.
So inside of this public variable section I'm going to add to our game sections uh different objectives
that can be fulfilled in our game.
So we'll have a table called objectives.
And in here we can have for example an objective of money.
And we want to have the text for this be um something like earn $500 to pay for rent.
And then another objective we could have would be to enter into the Krusty Krab.
So this would be text is equal to, uh, something like enter the Krusty Krab.
And then we can also have a Boolean in here to keep track of whether or not this objective was fulfilled
or not.
And we're going to set it to false for now.
So that means back in our game service we can replace this text here.
With the text that we supplied inside of the game sections.
So sections dot objectives.
Dot money dot text for this text.
And then we want to do the same thing here for the enter objective.
So sections dot objectives dot enter.
And we can set this equal to that text.
Now there's actually a couple of other parts we want to make references to.
One is going to be a part inside of the building that the players will touch.
Uh to start the first or basically the other scene of where they need to talk to Squidward.
So inside of the workspace there is a part and we can just call it something like Inside Part, and
that's in the workspace inside of the filtering folder, because that's where all of the invisible parts
are stored.
And inside of there there's a folder called touch parts and the part is inside.
And then we'll also have a touch part inside of the basement.
So we can call this basement part.
And that's equal to workspace dot filtering folder dot touch parts dot basement.
Because now what we can do is inside of our initialize section we can refer to something like our indoor
part or inside part and listen to when it is touched.
And when that gets touched, then we know to start a particular section.
And then the same thing for the basement part.
When that gets touched, then we know to start the basement section.
And last but not least, we can listen for when the escape part gets touched.
And that's where we will, you know, wrap up the game for this particular player.
Now we're also going to want to listen to when a player gets added into the game.
So that player added, we'll just connect a function to this and get the player.
And what we want to do is we want to listen for when that player has a character added to them, and
we'll connect our on character added function to this event.
So now we can go ahead and fill out this function.
And what we want to do in here is I'll actually create another function inside of this function.
And we'll just call it on humanoid humanoid death.
And what this function will do is it will get the player from this character using the player service,
get player from character and we'll pass the character object.
And we want to set the team of this player equal to the dead team.
So teams dot dead.
And then what we could do is we could just simply get the humanoid that is inside of our character,
listen to when it gets killed, and then we'll connect our on humanoid death function to this event.
Now, the other event we want to listen to inside of our initialize section is when a player gets added
to our dead team.
So teams dot dead dot player added.
We'll connect our on player added to team function.
And inside of here, what we want to do is we want to get the player's backpack and we want to call
the clear all children function on it so it destroys any tools that the player might have.
And then we want to check if this player has a character, because if they do, they might have a tool
equipped.
So we want to get this tool by accessing our player's character.
And we want to find first child which is a tool.
And if there is a tool then we need to destroy it.
So if tool then tool destroy.
So now we should be able to test this out by seeing if our loading screen fades out when we have loaded
into the game.
So if I hit play here, we're going to see our loading screen and it perfectly fades out because our
game has started and now we're stuck in spawn for now.
But at least so far, we can tell that everything is working as it's supposed to and we have no errors
in the console.
Thanks for sticking around for this lecture and I'll see you in the next one.